local UiAccount = {
  users = {},
  on = false,
  count = 0,
}

function UiAccount:init(ui, isTest)
  isTest = isTest or false
  self.index = 0
  local youka_auto = ui.youka_auto or 'off'
  if youka_auto == 'on' then
    if (not isTest) then
      self.on = true
    end
    self:getUsers(ui)
  end

  self.on = UiAccount:isUse()
end

function UiAccount:getUsers(ui)
  for i = 1,20 do
    local inputSwithch = 'yk_'..tostring(i)..'_auto'
    RxLog('UiAccount:getUsers inputSwithch='..inputSwithch)
    local on = ui[inputSwithch] or 'off'
    if on == 'on' then
      _setAccountConfig(ui, i)
    end
  end
end

function _setAccountConfig(ui, i)
  local u = {}
  local enable = true
  local accountKey = 'yk_'..tostring(i)..'_account'
  local account = ui[accountKey]
  if RxString:len(account) > 0 then
    u.account = account
  else
    enable = false
  end
  RxLog('UiAccount__setAccountConfig accountKey='..accountKey..'--enable='..tostring(enable)..'--account='..tostring(account))

  local passKey = 'yk_'..tostring(i)..'_pass'
  local pass = ui[passKey]
  if RxString:len(pass) > 0 then
    u.pass = pass
  else
    enable = false
  end
  RxLog('UiAccount__setAccountConfig passKey='..passKey..'--enable='..tostring(enable)..'--pass='..tostring(pass))

  local areaKey = 'yk_'..tostring(i)..'_area'
  RxLog('UiAccount__setAccountConfig areaKey='..areaKey..'--enable='..tostring(enable))

  if enable then
    u.area = ui[areaKey] or '1'
    RxLog('UiAccount__setAccountConfig i='..tostring(i))

    local count = UiAccount.count + 1

    u.index = count
    u.id = nil -- account_id
    u.level = 0 -- 等级
    u.power = nil -- 战力
    u.yubi = 0 -- 玉璧
    u.yuanbao = 0 --元宝
    u.greenstuff = 0 -- 蔬菜
    u.fullTask = false -- 满任务
    u.isCommander = false -- 团长

    UiAccount.users[count] = u
    UiAccount.count = count
    UiAccount.select = 0
  end
end

function UiAccount:isUse()
  local on = UiAccount.on
  RxLog('UiAccount:isUse on='..tostring(on))
  if not on then return false end

  local count = UiAccount.count
  RxLog('UiAccount:isUse count='..tostring(count))
  if count > 0 then
    return true
  end
  return false
end

return UiAccount